﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace SQLEditor.admin.service
{
    public class JsonUtil
    {

        public static string ToJson(object obj) {
            JsonSerializerSettings s = new JsonSerializerSettings();
            s.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            s.NullValueHandling = NullValueHandling.Ignore;
            string json = JsonConvert.SerializeObject(obj, Formatting.None, s);
            return json;
        }
        public static T fromJson<T>(string json) {
            JsonSerializerSettings s = new JsonSerializerSettings();
            s.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            s.NullValueHandling = NullValueHandling.Ignore;
            return JsonConvert.DeserializeObject<T>(json); 
        }
        public static object fromJson(string json, Type type) {
            JsonSerializerSettings s = new JsonSerializerSettings();
            s.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            s.NullValueHandling = NullValueHandling.Ignore;
            return JsonConvert.DeserializeObject(json, type, s);
        }
        public static object fromCodeJson(string json, Type type)
        {
            string str = decode(json);
            JsonSerializerSettings s = new JsonSerializerSettings();
            s.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            s.NullValueHandling = NullValueHandling.Ignore;
            return JsonConvert.DeserializeObject(str, type, s);
        }
        public static string ToCodeJson(object obj)
        {
            JsonSerializerSettings s = new JsonSerializerSettings();
            s.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            s.NullValueHandling = NullValueHandling.Ignore;
            string json = JsonConvert.SerializeObject(obj, Formatting.None, s);
            return code(json);
        }

        public static string code(string str) {
            if (string.IsNullOrEmpty(str))
                return null;
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(bytes);
        }

        public static string decode(string str)
        {
            if(string.IsNullOrEmpty(str))
                return null;
            byte[] outputb = Convert.FromBase64String(str);
            return Encoding.UTF8.GetString(outputb);
        }

        /// <summary>
        /// 过滤字符串
        /// </summary>
        public static string StringFilter(string str)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                char c = str.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
    }
}
